package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    public DvdUI()
    {
        dvdCtr = new DVDCtr();  
    }
    
    public void start()
    {
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =  writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else{if(choice == 2){
                   createDVD();
                }
                else{
                    if(choice == 3){
                        createCopy();
                    }
                    else{
                        if(choice == 4){
                             deleteDVD();
                        }
                        else{
                                if(choice == 5){
                                    System.out.println("\f");
                                    Scanner keyboard = new Scanner(System.in);
                                    listAllDvds();
                                    System.out.println("\nType 'Next' to continue");
                                    String next =  keyboard.next();
                                    
                                }
                                else{
                                    if(choice == 6){
                                        System.out.println("\f");
                                        Scanner keyboard = new Scanner(System.in);
                                        showCopies();
                                        System.out.println("\nType 'Next' to continue");
                                        String next = keyboard.next();
                                    }
                                    else{
                                        exit = true;
                                    }//end else
                                
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD");
            System.out.println(" (3) Create DVD copy");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Show copies of a DVD");
            System.out.println(" (7) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            return choise;
    }
  
    private void findDVD()
    {     
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Show DVD Information ***");
        System.out.print("\n");
        System.out.print("Please enter the ID of the DVD: ");
        long id = keyboard.nextLong();
        dvdCtr.printDVD(id);
        System.out.println("Please type 'Next' to continue");
        String next = keyboard.next();
    
    }
    
    private void createDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard.useDelimiter("\n");
        System.out.println("\f*** Register new DVD ***");
        System.out.println("Please enter the details of the new DVD");
        long id = inputDVDID();
        System.out.print("(2) Title: ");
        String title = keyboard.next();
        System.out.print("(3) Artist: ");
        String artist = keyboard.next();
        System.out.print("(4) Publication Date(dd/MM/yyyy): ");
        String publicationDate = keyboard.next();
       dvdCtr.createDVD(id,title,artist,publicationDate);
       System.out.println("Please type 'Next' to continue");
       String next = keyboard.next();
    }
    
    private long inputDVDID()
    {
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(1) ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    }
    
    private void createCopy()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Register new Copy ***\n");
        System.out.println("\n DVD list");
        listAllDvds();
        System.out.println("\nPlease enter the details of the new copy");
        long dvdId = inputDvdId();
        System.out.print("(2) Serial Number: ");
        String serialNumber = keyboard.next();
        System.out.print("(3) Purchase Date(dd/MM/yyyy): ");
        String purchaseDate = keyboard.next();
        double purchasePrice = inputPrice();
        dvdCtr.createCopy(dvdId,serialNumber,purchaseDate,purchasePrice);
        System.out.println("Please typem 'Next' to continue");
        String next = keyboard.next();
        
    }
    
    private long inputDvdId()
    {
        
    
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(1) DVD ID:  ");
           try{
               
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return id;
    
    }
    
    private double inputPrice()
    {
        
    
        boolean ok = false;
        double price = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.print("(4) Purchase Price:  ");
           try{
               
             price = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Please write a number");
             String input = keyboard.next();
           }
        }//end while
        return price;
    
    }
    
    private void deleteDVD()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Delete DVD ***\n");
        listAllDvds();
        System.out.print("Please enter the ID of the DVD you wish to delete: ");
        long id = keyboard.nextLong();
        dvdCtr.deleteDVD(id);
    }
    private void listAllDvds()
    {
        dvdCtr.listAllDvds();
        
    }
    private void showCopies()
    {
        
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter the ID of the DVD: ");
        long id = keyboard.nextLong();
        dvdCtr.listAllCopies(id);
        
    }
    
    public DVDCtr getDVDCtr()
    {
        return dvdCtr;
    }
    
}